package com.zh.rocketmq.mqmsg;

import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * @author Zhou
 * @date 14:49 2025/10/20
 */
@Component
@RocketMQMessageListener(topic = "DemoTopic", consumerGroup = "ConsumerOneGroup"
        ,consumeMode = ConsumeMode.ORDERLY, messageModel = MessageModel.CLUSTERING)
public class MqMessageConsumer implements RocketMQListener<String> {
    static Random random = new Random();

    @Override
    public void onMessage(String message) {
        try {
            Thread.sleep(500+random.nextInt(500));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        var str = String.format("%s [%s] Topic[%s] Received message: %s", LocalDateTime.now(), Thread.currentThread().getName(), "DemoTopic", message);
        System.err.println(str);
    }
}
